//
//  RegisterViewController.m
//  CoolFM
//
//  Created by 曹曹 on 16/8/29.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "RegisterViewController.h"
#import "AFNetworking.h"
#import "CheckCodeViewController.h"
#import "NSString+Regex.h"
#import "UIView+Frame.h"
#import "CheckCodeViewController.h"
#import "CFMacro.h"

@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneLabel;
@property (nonatomic, assign) BOOL isReset;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setTitle:@"验证手机号码"];
    self.view.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
}
- (IBAction)sendCheckCode:(id)sender {
    if ([_phoneLabel.text isEqualToString:@""]) {
        [self addPromptLabelWithTitle:@"请填写手机号码"];
    } else if (![NSString valiMobile:_phoneLabel.text]) {
        [self addPromptLabelWithTitle:@"请输入有效的手机号码"];
    } else {
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
            return parameters;
        }];
        
        NSDictionary *sendCode = @{@"contact":_phoneLabel.text,
                                 @"reason":@"1",};
        if (self.isFogetPassword) {
            sendCode = @{@"contact":_phoneLabel.text,
                         @"reason":@"5",};
            self.isReset = YES;
            [self.navigationController setTitle:@"忘记密码"];
        }
        /**
         5是忘记密码,push到注册界面
         1是注册新用户
         */
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [manager.requestSerializer setValue:@"38" forHTTPHeaderField:@"Content-Length"];
        [manager.requestSerializer setValue:@"api.kufm.cn" forHTTPHeaderField:@"Host"];
        [manager.requestSerializer setValue:@"ios/4.3.0.0.0/appstore/release" forHTTPHeaderField:@"X-API-ID"];
        [manager.requestSerializer setValue:@"d0ce47483e6728610311619cca3fb195" forHTTPHeaderField:@"X-APP-ID"];
        
        [manager POST:@"http://api.kufm.cn/v2/user/send_check_code"
           parameters:sendCode
             progress:^(NSProgress * _Nonnull uploadProgress) {
             } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                 NSLog(@"请求成功:%@", responseObject);
                 CheckCodeViewController *checkCode = [[CheckCodeViewController alloc] init];
                 checkCode.contact = _phoneLabel.text;
                 checkCode.isResetPassword = _isReset;
                 [self.navigationController pushViewController:checkCode animated:YES];
             } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                 NSLog(@"请求失败:%@", error);
                 [self addPromptLabelWithTitle:@"该手机号已注册"];
             }];
    }
}

- (void)addPromptLabelWithTitle:(NSString *)title {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, -20, self.view.gx_width, 20)];
    label.text = title;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:12];
    label.gx_centerX = self.view.gx_centerX;
    label.backgroundColor = [UIColor grayColor];
    [self.view addSubview:label];
    // 出现
    [UIView animateWithDuration:0.5 animations:^{
        label.gx_y = 0;
        // 显示2秒
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 delay:2 options:UIViewAnimationOptionCurveLinear animations:^{
            label.gx_y = -20;
        } completion:^(BOOL finished) {
            [label removeFromSuperview];
        }];
    }];
}

- (void)viewDidDisappear:(BOOL)animated {
    NSLog(@"销毁");
    [super viewDidDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
